/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import pojo.SanPham;
import util.HibernateUtil;

/**
 *
 * @author LaiHuynh
 */
public class SanPhamDAO {

    public static List<SanPham> layDanhSachSanPham() {
        List<SanPham> ds = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            String hql = "select sp from SanPham sp";
            org.hibernate.Query query = session.createQuery(hql);
            ds = query.list();
        } catch (HibernateException ex) {
            //Log the exception 
            System.err.println(ex);
        } finally {
            session.close();
        }
        return ds;
    }
    public static List<SanPham> laysptheoch(int mach){
        List<SanPham> ds=null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            String hql = "select sp from SanPham sp where sp.cuaHang.maCuaHang="+mach;
            org.hibernate.Query query = session.createQuery(hql);
            ds = query.list();
        } catch (HibernateException ex) {
            //Log the exception 
            System.err.println(ex);
        } finally {
            session.close();
        }
        return ds;
    }
    public static SanPham layThongTinSanPham(int maSanPham) {
        SanPham sp = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            sp = (SanPham) session.get(SanPham.class, maSanPham);
        } catch (HibernateException ex) {
            //log the exception
            System.err.println(ex);
        } finally {
            session.close();
        }
        return sp;
    }

    public static List<SanPham> timKiemSanPham(String tenSanPham, int maHangsx,double gia,int soluong) {
        List<SanPham> ds = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            //String hql = "select sp from SanPham sp where sp.tenSanPham like :tenSanPham and sp.hangSanXuat.maHangsx =:maHangsx";
            String hql = "select sp from SanPham sp where sp.tenSanPham like '%"
                    + tenSanPham+"%'";
            if(maHangsx !=0){
                   hql=hql + "and sp.hangSanXuat.maHangsx ="
                    + maHangsx;
            }
            if(gia!=0){
                if(gia==1){
                    hql=hql+" and sp.giaBan <="+1000000;
                }else if(gia==2){
                    hql=hql+" and sp.giaBan <="+5000000+"and sp.giaBan >"+1000000;
                }else if(gia ==3){
                    hql=hql+" and sp.giaBan >"+5000000;
                }
            }
            if(soluong!=0){
                if(soluong==1){
                    hql=hql+"and sp.soLuongTon <="+100;
                }else if(soluong ==2){
                     hql=hql+"and sp.soLuongTon <="+300+"and  sp.soLuongTon >"+100;
                }else if(soluong ==3){
                     hql=hql+"and sp.soLuongTon >"+300;
                }
                
            }
            org.hibernate.Query query = session.createQuery(hql);
            //query.setString("tenSanPham", "%"+tenSanPham+"%");
            //query.setInteger("maHangsx", maHangsx);
            ds = query.list();
        } catch (HibernateException ex) {
            //Log the exception 
            System.err.println(ex);
        } finally {
            session.close();
        }
        return ds;
    }

    public static void themSanPham(SanPham sp) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        session.save(sp);
        session.getTransaction().commit();
        session.close();
    }

    public static boolean xoaSanPham(int maSanPham) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        SanPham sp = SanPhamDAO.layThongTinSanPham(maSanPham);
        if (sp == null) {
            return false;
        }
        session.beginTransaction();
        session.delete(sp);
        session.getTransaction().commit();
        session.close();
        return true;
    }

    public static void capNhatThongTinSanPham(SanPham sp) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        session.update(sp);
        session.getTransaction().commit();
        session.close();
    }
    public static int KtraTenSp(String tensp,int mach){
        int kq=1;
        List<SanPham> ds = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
         String hql = "select sp from SanPham sp where sp.tenSanPham ='"
                    + tensp+"' and sp.cuaHang.maCuaHang="+mach;
         org.hibernate.Query query = session.createQuery(hql);
         ds = query.list();
         session.close();
         if(ds.size()==0){
             kq=0;
         }
        return kq;
    }
    public static int LayMa(){
         int kq=0;
        
        Session session = HibernateUtil.getSessionFactory().openSession();
         String hql = "select max(sp.maSanPham) from SanPham sp ";
         org.hibernate.Query query = session.createQuery(hql);
         kq =(Integer) query.uniqueResult(); 
         session.close();
        return kq;
    }
}
